<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@include file="../common/NavigationLeft.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>二维码管理系统</title>
	<style type="text/css">
		.select2-container--open{
			z-index:9999999
		}
	</style>
</head>
<body style="height: 100%">
	<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
		<h2 class="page-header">资产列表</h2>
			<div class="row m-b-sm m-t-sm">
				<div class="col-md-1">
					<a href="listAsset"><button type="button"
							class="btn btn-default btn-sm">刷新</button></a>
				</div>
				<form class="form-horizontal" action="listAsset.action"
					method="post" id="searchForm">
					<%--<s:if test="session.user.role == 0">--%>
						<div class="col-md-5">
							<button class="btn btn-default btn-sm" type="button"
									id="deleteMany_btn">删除</button>
							<button class="btn btn-default btn-sm" id="QRcode_btn"
									type="button">生成二维码</button>
							<a href="to_addAsset.action"><button type="button"
																 class="btn btn-default btn-sm" id="entry_btn">添加</button></a>
							<button type="button" class="btn btn-default btn-sm" id="download_btn" onclick="download();">导出资产表格</button>
							<button type="button" class="btn btn-default btn-sm" id="fileUpload_btn">批量添加</button>
						</div>
					<%--</s:if>--%>
					<div class="col-md-4 f-r">
						<div class="input-group">
							<input type="text" id="searchTemp" class="form-control" value="${temp }" name="temp"
								placeholder="请输入资产编号/使用人" style="height:30px;"> <span
								class="input-group-btn"><button
									class="btn btn-primary btn-sm" type="submit" id="search_btn">搜索</button> </span>
						</div>
					</div>
				</form>
			</div>
			<div class="placeholder"></div>
		<!-- 		<table id="" class="table table-striped" cellspacing="0" width="100%"> -->
		<form class="form-horizontal" action="genQRcode.action" method="post" id="asset_Form" target="_blank">
		<table id="listTable" class="table table-bordered table-striped table-hover" cellspacing="0">
			<thead>
				<tr>
					<th>
						<input autocomplete="off" type="checkbox" id="CheckedAll"
						onclick="checkAll()">
					</th>
					<th>资产编号</th>
					<th>资产型号</th>
					<!-- <th>资产类别</th> -->
					<th class="min-tablet-p dropdown selectdown">
                        <div class="table_dropdown dropdown1">
						<a data-toggle="dropdown" class="dropdown-toggle" href="javascript:void(0);" >资产类别 <span class="caret"></span></a>	
						<ul role="menu" class="dropdown-menu">
                            <li class="active" value="" ><a href="javascript:void(0);">全部</a></li>
                            <li value="0"><a href="javascript:void(0);">笔记本</a></li>
                            <li value="1"><a href="javascript:void(0);">台式电脑主机</a></li>
                            <li value="2"><a href="javascript:void(0);">显示器</a></li>
                            <li value="3"><a href="javascript:void(0);">打印机</a></li>
                            <li value="4"><a href="javascript:void(0);">无线设备</a></li>
                            <li value="5"><a href="javascript:void(0);">交换机</a></li>
                            <li value="6"><a href="javascript:void(0);">移动存储设备</a></li>
                            <li value="7"><a href="javascript:void(0);">软件</a></li>
                            <li value="8"><a href="javascript:void(0);">其他配件</a></li>
                        </ul>
                        </div>
                    </th>
					<th>使用人</th>
					<!-- <th>资产状况</th> -->
					<th class="min-tablet-p dropdown selectdown">
                        <div class="table_dropdown dropdown2">
						<a data-toggle="dropdown" class="dropdown-toggle" href="javascript:void(0);" >资产状况 <span class="caret"></span></a>	
						<ul role="menu" class="dropdown-menu">
                            <li class="active" value="" ><a href="javascript:void(0);">全部</a></li>
                            <li value="0"><a href="javascript:void(0);">使用中</a></li>
                            <li value="1"><a href="javascript:void(0);">已分配</a></li>
                            <li value="2"><a href="javascript:void(0);">维修中</a></li>
                            <li value="3"><a href="javascript:void(0);">备用</a></li>
                            <li value="4"><a href="javascript:void(0);">报废</a></li>
                        </ul>
                        </div>
                    </th>
					<th>采购日期</th>
					<th>使用日期</th>
					<th>操作</th>
				</tr>
			</thead>
			<tbody>
			</tbody>
			
		</table>
		</form>
	</div>
	<div id="batch_import" class="edit-detail layer_panel"
		style="height: 80px;display:none" >
		<form method="post" id="file_upload_form" class="m-t"
			action="fileInputAsset.action">			
			<div class="form-group">
				<label class="col-sm-4 control-label"><span
					class="text-danger">*</span>文件：</label>
				<input  class="row col-sm-6 control-label" type="file" name="fileName" id="file_input" />
			</div>
		</form>
	</div>
	<div class="edit-detail layer_panel" id="File_upload_panel" >
		<form action="upload.action" method="post" id="uploadFileForm" enctype="multipart/form-data">
			<div>
				<label class="col-sm-12 control-label">请选择文件（.csv文件）</label>
			</div>
			<div class="col-sm-12">
				<s:file class="col-sm-12" type="file" name="file" id="file"
					onchange="changeFileName()"></s:file>
			</div>
			<div style="display:none"><p class='col-sm-12' style='color:red'>请选择文件!</p></div>
			<input type="hidden" id="fileName" name="fileName" value="" />
		</form>
	</div>
	<div class="edit-detail layer_panel" id="assignToUser_panel" >
		<label >选择使用人</label>
		<%--class="form-control select2"--%>
		<select id="userId" name="userId" lang="zh-CN" class="select2" style="width: 100%;">
		</select>
	</div>
<input id="userRole" type="hidden" value="${session.user.role}">
</body>
<script type="text/javascript">


$(function(){
	var table = "";
	initTable();
	$("#search_btn").click(function(){
		initTable();
	});
	var select2;

	$(document).on("click",".dropdown1 .dropdown-menu a",function(){
		$(this).parent().addClass("active").siblings().removeClass('active');
		$(".table_dropdown").removeClass("open");
		var equipGenre = $(this).parent().attr("value");
		var status = $(".dropdown2 .dropdown-menu li[class='active']").attr("value");
		initTable(equipGenre,status);
		
	});
	$(document).on("click",".dropdown2 .dropdown-menu a",function(){
		$(this).parent().addClass("active").siblings().removeClass('active');
		$(".table_dropdown").removeClass("open");
		var status = $(this).parent().attr("value");
		var equipGenre = $(".dropdown1 .dropdown-menu li[class='active']").attr("value");
		initTable(equipGenre,status);
	});

	function initTable(equipGenre,status) {
		var temp = $("#searchTemp").val();
		$.fn.dataTable.ext.errMode = function(s,h,m){
            layer.alert('数据加载失败');
        }
		 table = $('#listTable').DataTable( {
			 "destroy" : true,
			"paging" : true,
			"lengthChange" : true,
			"searching" : false,
			"ordering" : false,
			"info" : true,
			"autoWidth" : false,
			"Filter" : false,
			responsive : true,
			"pagingType" : "full_numbers",
			"dom" : 'rtilp',
			"language" : {
				"url" : "/js/datatables/jquery.dataTables.zh"
			},
	        "processing": true,
	        "serverSide": true,
	        "ajax": {
	   	          type: 'POST',  
	   	          url: 'ajax_listAllAssetData.action',  
	   	       		data:{"temp":temp,"equipGenre":equipGenre,"status":status},
	   	          //contentType: 'application/json',  
	   	          timeout:60000  
	   	      },
             "drawCallback":function () {
            	 $(".Del_li").click(function() {
					 var assetId = $(this).attr("assetId");
            			layer.confirm('确认删除该设备？', {
            				icon : 3,
            				title : '删除设备'
            			}, function(index) {
	            				$.ajax({
	                				type : 'POST',
	                				url : 'ajax_deleteAsset.action',
	                				data : {
	                					"id" : assetId
	                				},
	                				success : function(result) {
	                					if (result.operaRes == "1") {
	                						$("#search_btn").click();
	                						//layer.msg('删除成功');
	                					} else {
	                						$("#search_btn").click();
	                						//layer.msg('删除失败');
	                					}
	                				}
	                			}); 
            		    	});
            	});
				 $(".assign_li").click(function () {
                     var assetId = $(this).attr("assetId");
                     $.ajax({
                         type : 'POST',
                         url : '/ajax_listUserData.action',
                         data : {
//			"queryUserName" : ""
                         },
                         success : function(result) {
                             $("#userId").select2(
                                 {
                                     language: "zh-CN",
                                     placeholder:"请选择",
                                     data:result,
                                 }
                             );
                             var userId = $("#userId").val();
							 $("#userId").on('select2:select', function (evt) {
                                 userId = $("#userId").val();
                             });
                             layer.open({
                                 title: '选择使用人',
                                 type: 1,
                                 area: ['400px','200px'],
                                 content: $("#assignToUser_panel"),
                                 btn: ['确定', '取消'],
                                 yes: function(index, layero) {
                                     layer.close(index);
                                     $.ajax({
                                         type : 'POST',
                                         url : 'ajax_assignAsset.action',
                                         data : {
                                             "id" : assetId,
                                             "userId" : userId
                                         },
                                         success : function(result) {
                                             layer.msg("操作成功");
                                             table.page(table.page()).draw(false);
                                         },
                                         error:function () {
                                             layer.msg("操作失败");
                                         }

                                     });
                                 },
                                 cancel: function(index) {}
                             });
                         },
                         error:function () {
                             layer.alert("操作失败");
                         }
                     });
				 })
             },
	        "columns" : [
							{
								"data" : function(data) {
										return '<input autocomplete="off" name="checkedIds" type="checkbox" value="'+ data.id+'">';
								}
							},
							{
								"data" : function(data) {
									if(data.equipIdentifier != null){
										return data.equipIdentifier;
									}else{
										return "--";
									}
								}
							},
							{
								"data" : function(data) {
									if(data.equipModel != null){
										return data.equipModel;
									}else{
										return "--";
									}
								}
							},
							{
								"data" : function(data) {
									if(data.equipGenre != null){
										return data.equipGenre;
									}else{
										return "--";
									}
								}
							},
							{
								"data" : function(data) {
									if(data.usePerson != null){
										return data.usePerson;
									}else{
										return "--";
									}
								}
							},
							{
								"data" : function(data) {
									if(data.status != null){
										return data.status;
									}else{
										return "--";
									}
								}
							},
							{
								"data" : function(data) {
									if(data.buyTime != null){
										var now = new Date(data.buyTime);
										var nowStr = now.format("yyyy-MM-dd");
										return nowStr;
									}else{
										return "--";
									}
								}
							},
							{
								"data" : function(data) {
									if(data.usedTime != null){
										var now = new Date(data.usedTime);
										var nowStr = now.format("yyyy-MM-dd");
										return nowStr;
									}else{
										return "--";
									}
								}
							},
							{
								"data" : function(data){
									var modifyOpe ="";
									var deleteOpe = "";
									var assignOpe = "";
									var userRole = $("#userRole").val();

									if(userRole == "0"){
										modifyOpe = '<li><a class="" href="to_addAsset.action?id='+data.id+'" onclick="">修改</a></li>';
										assignOpe = '<li><a class="assign_li" href="javascript:void(0);" assetId="'+data.id+'">分配</a></li>';
										deleteOpe = '<li><a class="Del_li" href="javascript:void(0);" assetId="'+data.id+'">删除</a></li>';
									}
									return '<div class="btn-group">'+
									'<button data-toggle="dropdown"'+
										'class="btn btn-primary btn-xs dropdown-toggle" type="button">'+
										'<i class="fa fa-cog"></i> 操作 <span class="caret"></span>'+
									'</button>'+
									'<ul class="dropdown-menu">'+
										'<li><a class="" href="assetInfo.action?id='+data.id+'">查看</a></li>'+
											modifyOpe+deleteOpe+assignOpe
									'</ul>'+
								'</div>';
								}
							}
							
					]
	    	} );
		return table;
	}
})


$(function(){
	var tip="${tip}";
	if(tip!=""){
		layer.msg(tip,{area: ['300px','50px']});
	}
	$("#QRcode_btn").click(function() {
		checkboxlen = 0;
		$("td :checkbox").each(function(index, ele) {
			if ($(this).prop("checked")) {
				checkboxlen++; 
			}
		});
		if(checkboxlen<1){
			layer.msg("至少勾选一项");
		}else{
			$("#asset_Form").submit();
		}
	});
	////////////////////////
	
	
});


function download(){
	window.location.href="exportAssetExcel.action";
}
	//删除多个
	$("#deleteMany_btn").click(function() {
		var ids = selectIds();
			layer.confirm('确认删除选中的设备？', {
				icon : 3,
				title : '删除设备'
			}, function(index) {
					$.ajax({
						type : 'POST',
						url : 'ajax_deleteAssetMany.action',
						data : {
							"ids" : ids
						},
						success : function(result) {
							if (result.operaRes == "1") {
								
								//layer.msg('删除成功');
								$("#search_btn").click();
							} else {
								//layer.msg('删除失败');
								$("#search_btn").click();
							}
						}
					});
		    	});
	})

	function checkAll() {
		if ($("#CheckedAll").prop("checked")) {
			$(":checkbox").prop("checked", true);
		} else {
			$(":checkbox").prop("checked", false);
		}
	}
	function selectIds() {
		var assIds = new Array();
		var idString = "";
		$("td :checkbox").each(function(index, ele) {
			if ($(this).prop("checked")) {
				assIds.push($(this).attr("value"));
			}
		})
		for (var i = 0; i < assIds.length; i++) {
			idString = assIds[i] + "," + idString;
		}
		return idString;
	}
	
	function changeFileName(){
		var filePath = $("#file").val();
		var fileName = filePath.substring(filePath.lastIndexOf("\\") + 1);
		$("#fileName").val(fileName);
		$("#file").parent().next().hide();
	}
	
	$("#fileUpload_btn").click(function(){		
		layer.open({
            title: '文件批量导入',
            type: 1,
            area: ['400px','200px'],
            content: $("#File_upload_panel"),
            btn: ['确定', '取消'],
            yes: function(index, layero) {
            	    if($("#file").val()!=''){
                        $("#uploadFileForm").submit();                      
            	    }
            	    else{
            	    	$("#file").parent().next().show();
            	    }
            	},              
            cancel: function(index) {}
        });
	});
	Date.prototype.format = function(format){
		var o = {
		"M+" : this.getMonth()+1, //month
		"d+" : this.getDate(), //day
		"h+" : this.getHours(), //hour
		"m+" : this.getMinutes(), //minute
		"s+" : this.getSeconds(), //second
		"q+" : Math.floor((this.getMonth()+3)/3), //quarter
		"S" : this.getMilliseconds() //millisecond
		}

		if(/(y+)/.test(format)) {
		format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
		}

		for(var k in o) {
			if(new RegExp("("+ k +")").test(format)) {
				format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
			}
		}
		return format;
	} 
</script>
</html>